Skip to content

fix(watch): clear ambient git env so repo detection honors current_dir - #171

Merged
sunerpy merged 1 commit into
mainfrom
fix/watch-testdir-unique
Jul 24, 2026
Merged

fix(watch): clear ambient git env so repo detection honors current_dir#171
sunerpy merged 1 commit into
mainfrom
fix/watch-testdir-unique

Conversation

@sunerpy

@sunerpy sunerpy commented Jul 24, 2026

Copy link
Copy Markdown
Owner

Summary

is_git_repo / git_worktree_root set current_dir(dir) but inherited the ambient git repo-selection env (GIT_DIR, GIT_WORK_TREE, ...). When codegraph runs inside a git hookpost-commit/post-merge/post-checkout invoke codegraph sync — git exports those vars, so git answered "is THIS dir a repo? / what's its worktree root?" for the hook's repo, ignoring the directory we actually asked about.

Root cause

git prioritizes GIT_DIR/GIT_WORK_TREE over current_dir. Any git invocation that only sets current_dir is therefore wrong whenever those vars are present in the environment (git hooks, and — incidentally — a git push pre-push hook running the test suite).

Fix

  • New git_command(dir) in git.rs: builds the git command rooted at dir and env_removes the 7 repo-selection vars (GIT_DIR, GIT_WORK_TREE, GIT_COMMON_DIR, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_NAMESPACE, GIT_PREFIX), so discovery resolves purely from current_dir.
  • Routed is_git_repo, git_output, git_worktree_root, and the two test git_init helpers through it.

Verification

  • Repro'd the bug by injecting GIT_DIR/GIT_WORK_TREE (repo query flips true↔false).
  • With those vars set (simulating a hook), the full codegraph-watch lib test suite: 86/86 pass (previously 6 failed).
  • Default-env cargo test --workspace: green, no regression.
  • cargo clippy --all-targets -D warnings, cargo fmt --check, scripts/guardrail.sh: all pass.
  • The pre-push hook (which itself injects the git env) now passes without --no-verify.

is_git_repo/git_worktree_root set current_dir but inherited GIT_DIR/
GIT_WORK_TREE, so when codegraph runs inside a git hook (post-commit/
post-merge/post-checkout invoke 'codegraph sync') git answered repo
queries for the hook's repo instead of the queried directory. Route all
git invocations through git_command(), which strips the repo-selection
env vars so discovery resolves purely from current_dir.
@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #171   +/-   ##
=======================================
  Coverage   95.27%   95.27%           
=======================================
  Files         124      124           
  Lines       61312    61316    +4     
=======================================
+ Hits        58412    58416    +4     
  Misses       2900     2900           
Files with missing lines Coverage Δ
crates/codegraph-watch/src/git.rs 94.42% <100.00%> (+0.11%) ⬆️
crates/codegraph-watch/src/worktree.rs 92.48% <100.00%> (-0.12%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sunerpy
sunerpy merged commit 9a9e67f into main Jul 24, 2026
7 checks passed
@sunerpy
sunerpy deleted the fix/watch-testdir-unique branch July 24, 2026 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant